#ifndef __TIMERSTUFF
#define __TIMERSTUFF

/*
 *   This file is part of ed_pvr.
 *
 *   ed_pvr is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   ed_pvr is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with ed_pvr.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef __cplusplus
    extern "C" {
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>

#define TIMER_USE_DD_PROGID			0xFFFFFFFF
#define TIMER_MAX_TIMERS			254

#define TIMER_STATUS_INVALID		0x00
#define TIMER_STATUS_WAITING		0x01
#define TIMER_STATUS_BUSY			0x02
#define TIMER_STATUS_DONE			0x03

#define TIEMR_STRUCT_MAX_STRLEN		257
#define TIMER_EXTRA_CAT_MAX			5

struct timer_struct_extra
{
	uint8_t		category_count;

	char 		icon_path[TIEMR_STRUCT_MAX_STRLEN];
	char 		category[TIMER_EXTRA_CAT_MAX][TIEMR_STRUCT_MAX_STRLEN];
	int			p_rating;
	int			s_rating;
	int64_t		org_air_date;
	//time_t		org_air_date;
	int			series_num;
	int			ep_num;
	int			ep_part;
//	uint8_t		last;
	uint64_t	epg_count;
};
typedef struct timer_struct_extra TIMER_STRUCT_EXTRA;


struct timer_struct_send
{
	uint8_t		is_last;
	uint32_t	id;
	uint8_t 	status;
	uint64_t	channel_num;
	int64_t		start_epoch;
	int64_t		end_epoch;
	char 		url[4096];
	char 		channel_name[TIEMR_STRUCT_MAX_STRLEN];
	char 		title[TIEMR_STRUCT_MAX_STRLEN];
	char 		plot[TIEMR_STRUCT_MAX_STRLEN * 10];
	char 		plot_outline[TIEMR_STRUCT_MAX_STRLEN];
	char 		genre[TIEMR_STRUCT_MAX_STRLEN];
	char 		sub_genre[TIEMR_STRUCT_MAX_STRLEN];

};
typedef struct timer_struct_send TIMER_STRUCT_SEND;

struct timer_struct
{
	TIMER_STRUCT_SEND data;

	char 		recording_dir[TIEMR_STRUCT_MAX_STRLEN];
	char 		filename[TIEMR_STRUCT_MAX_STRLEN];
	int			socket;
	pthread_t 	read_thread;
	void 		*extra_data;

	struct 		timer_struct *next;
	struct 		timer_control_struct *parent;
};
typedef struct timer_struct TIMER_STRUCT;


struct timer_control_struct
{
/*
	char			*record_dir;
	char			*record_exe_path;
	char			*record_exe_name;
*/
	char			*state_filename;
	int				state_fileh;
	uint8_t			die;

	uint32_t		post_pad;
	uint32_t		pre_pad;

	pthread_mutex_t timer_mutex;
	pthread_mutex_t file_mutex;
	pthread_t 		timer_thread;

	uint32_t		count;
	uint64_t		sequence_num;
	TIMER_STRUCT	*timers[TIMER_MAX_TIMERS];

//	int				(*cb_timer_change_state)(TIMER_STRUCT *);
//	NCURSES_SCREEN *screen;
	void			*extra_data;
};
typedef struct timer_control_struct TIMER_CONTROL_STRUCT;

struct timer_arg_struct
{
	uint32_t		pre_pad;
	uint32_t		post_pad;
	char			*state_filename;
	char			*record_dir;
	char			*record_exe_path;
	char			*record_exe_name;
	void			*extra_data;
};
typedef struct timer_arg_struct TIMER_ARG_STRUCT;


TIMER_CONTROL_STRUCT * timers_init(TIMER_ARG_STRUCT *);
int timers_load_file(TIMER_CONTROL_STRUCT *);
int timers_save_file(TIMER_CONTROL_STRUCT *);
int timer_add_timer(TIMER_CONTROL_STRUCT *, TIMER_STRUCT *);
int timer_del_timer(TIMER_CONTROL_STRUCT *, int32_t);
//int timer_stop_timer(TIMER_CONTROL_STRUCT *, int32_t);
TIMER_STRUCT * timer_list_timer(TIMER_CONTROL_STRUCT *);
TIMER_STRUCT * timer_get_timer_by_id(TIMER_CONTROL_STRUCT *, uint32_t);
int timer_count_timer(TIMER_CONTROL_STRUCT *);
void timer_print_timer(TIMER_STRUCT *);
void timer_print_list(TIMER_CONTROL_STRUCT *);
void timer_print_timer2(TIMER_STRUCT *);

int timer_send_timer_socket(TIMER_STRUCT *, int);

//uint32_t timer_get_channel_from_id(

#ifdef __cplusplus
    }
#endif

#endif
